package com.myjob.controller;

import com.myjob.entity.Member;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.MemberService;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Chen Yuan
 * @create 2021-01-23  14:58
 */
@RestController
@Slf4j
@RequestMapping("/company")
public class MemberController {
	@Autowired
	private MemberService memberService;

	/**
	 * 查询所有的会员信息
	 */
	@GetMapping("/memberInfo")
	public Result selectAllMembers(){
		Result result = null;
		try {
			result = memberService.selectAllMembers();
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询异常");
		}
		return result;
	}

	/**
	 * 按照会员类型的名称查询会员信息
	 * @param typeName 会员类型名称
	 */
	//@GetMapping("/memberInfo")
	//public Result selectOneMember(@RequestParam("typeName") String typeName){
	//	Result result = null;
	//	try {
	//		Member member = null;
	//		if (UtilValidate.isNotEmpty(typeName)){
	//			member = memberService.selectOneMember(typeName);
	//		}
	//		result = Result.succeed(member);
	//	} catch (Exception e) {
	//		e.printStackTrace();
	//		throw new OptionException("查询异常");
	//	}
	//	return result;
	//}

	/**
	 * 修改会员信息
	 */
	@PostMapping("/memberInfo")
	public Result updateMember(@RequestBody Member member){
		Result result = null;
		try {
			int update = memberService.updateMember(member);
			if (update == 1){
				result = Result.succeed("修改成功");
			}else {
				result = Result.failed("修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("修改异常");
		}
		return result;
	}
}
